/**
 * 
 */
package com.toncen.samepms.interceptor;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.toncen.samepms.common.ComitConstant;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.config.entities.ResultConfig;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.interceptor.PreResultListener;
import com.sky.common.file.FileUtils;
import com.sky.common.string.StringUtils;

/**
 * 返回结果拦截器：在日志中记录action跳转的路径，方便调试
 * 
 * @author tch
 * 
 */
public class PreResultInterceptor implements Interceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 656861533783772732L;

	protected Log logger = LogFactory.getLog(this.getClass());

	/**
	 * 
	 */
	public PreResultInterceptor() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.interceptor.Interceptor#destroy()
	 */
	public void destroy() {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.interceptor.Interceptor#init()
	 */
	public void init() {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.interceptor.Interceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
	 */
	public String intercept(ActionInvocation invocation) throws Exception {
		invocation.addPreResultListener(new PreResultListener() {
			public void beforeResult(ActionInvocation invocation, String resultCode) {
				Map<String, ResultConfig> resultsMap = invocation.getProxy().getConfig().getResults();
				ResultConfig finalResultConfig = resultsMap.get(resultCode);
				outInfo(invocation.getAction(), "=======================================================================================");
				outInfo(invocation.getAction(), invocation.getProxy().getNamespace());
				outInfo(invocation.getAction(), invocation.getProxy().getActionName());
				outInfo(invocation.getAction(), invocation.getProxy().getMethod());
				if (finalResultConfig != null) {// 有返回结果
					outInfo(invocation.getAction(), finalResultConfig.getName() + "(" + finalResultConfig.getClassName() + "):");
					outInfo(invocation.getAction(), finalResultConfig.getParams().get("location"));
					if (finalResultConfig.getLocation() != null) {// 获取配置文件位置
						String location = finalResultConfig.getLocation().getURI();
						String classpath = FileUtils.getClassPath();
						location = StringUtils.replaceAll(location, "file:", "");
						classpath = StringUtils.replaceAll(classpath, "\\", "/");
						outInfo(invocation.getAction(), StringUtils.replaceAll(location, classpath, ""));
						outInfo(invocation.getAction(), finalResultConfig.getLocation().getLineNumber());
					}
				}
				outInfo(invocation.getAction(), "=======================================================================================");
			}
		});
		return invocation.invoke();
	}

	/**
	 * @param targetObj
	 * @param content
	 */
	private void outInfo(Object targetObj, Object content) {
		if (ComitConstant.isDeveloperMode()) {
			System.out.println("[" + targetObj.getClass().getSimpleName() + "]:" + content);
		}
	}
}
